package module4.homework;

import java.io.*;
import java.util.Scanner;

public class CopyTest implements Runnable {

    @Override
    public void run() {
        System.out.println("请分别输入拷贝的源目录和目标目录：");
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();
        show(str1, str2);
        System.out.println("拷贝完毕！");
    }

    public /*static*/ void show(String s1, String s2) {
        File f1 = new File(s1);
        if (!f1.exists()) {
            f1.mkdir();
        }
        File f2 = new File(s2);
        if (!f2.exists()) {
            f2.mkdir();
        }

        File[] fl1 = f1.listFiles();
        for (File file : fl1) {
            String str11 = file.getName();
            String str12 = file.getAbsolutePath();
            if (file.isFile()) {
                BufferedInputStream bis1 = null;
                BufferedOutputStream bos1 = null;
               /* FileOutputStream fos1 = null;
                FileInputStream fis1 = null;*/
                try {
                    /*fis1 = new FileInputStream(str12);
                    fos1 = new FileOutputStream(s2 + "/" + str11);*/
                    bis1 = new BufferedInputStream(new FileInputStream(str12));
                    bos1 = new BufferedOutputStream(new FileOutputStream(s2 + "/" + str11));
                    byte[] b1 = new byte[1024];
                    int res = 0;
                    while ((res = bis1.read(b1)) != -1 ) {
                        bos1.write(b1, 0, res);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null != bos1) {
                        try {
                            bos1.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != bis1) {
                        try {
                            bis1.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else {
                File f11 = new File(s2 + "/" + str11);
                f11.mkdir();
                String str13 = f11.getPath();
                show(str12, str13);
            }

        }



    }

    public static void main(String[] args) {

        //请用户输入两个文件拷贝的路径
        /*System.out.println("请分别输入拷贝的源目录和目标目录：");
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();
        show(str1, str2);
        System.out.println("拷贝完毕！");*/
    }
}

